#include "FtpServer.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>


int process_args( FtpServer* pServer, int argc, char* argv[] )
{
	int i;

	for( i = 1; i < argc; i++ )
	{
		int arg_avail = i < (argc - 1);

		if (!strcmp("-userlist", argv[i])) {
			if (!arg_avail)
				return -1;

			FtpServer_SetUserlist(pServer, argv[++i]);
		}
		else if(!strcmp("-port",argv[i]))
		{
			if( !arg_avail )
				return -1;

			FtpServer_SetPort( pServer,strtol(argv[++i],NULL,10) );
		}
		else if(!strcmp("-anonymous",argv[i]))
		{
			// enable anonymous logins
			FtpServer_SetAnonymous( pServer, 1 );
		}
		else
		{
			printf("npftp: unknown argument '%s'\n",argv[i]);
			return -1;
		}
	}

	return 0;
}


int main(int argc, char* argv[])
{
	FtpServer srv;

	// create server

	FtpServer_Create(&srv);

	// process arguments
	FtpServer_SetPort(&srv,3500); // 21 privileged under linux
	if( process_args( &srv, argc, argv ) < 0 )
	{
		printf("npftp: could not parse arguments\n" );
		FtpServer_Destroy(&srv);
		return 1;
	}

	// setup server
	if( -1 == FtpServer_Start(&srv) )
	{
		printf("Server Close\n");
		FtpServer_Destroy(&srv);
		return 1;
	}

	// run server
	while(FtpServer_IsRunning(&srv))
		FtpServer_HandleEvents(&srv);

	FtpServer_Destroy(&srv);
	return 0;
}


